package cc.rengu.redp.bizimpl.upmp.controller;


import cc.rengu.redp.common.annotation.Log;
import cc.rengu.redp.common.controller.BaseController;
import cc.rengu.redp.common.domain.QueryRequest;
import cc.rengu.redp.common.exception.RedpException;
import cc.rengu.redp.bizimpl.upmp.domain.IcEmvInfo;
import cc.rengu.redp.bizimpl.upmp.domain.IcEmvInfoBean;
import cc.rengu.redp.bizimpl.upmp.service.IcEmvInfoService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * @author LiuYuLiang.LL
 */
@Slf4j
@Validated
@Api(tags = "参数管理:IC卡参数管理")
@RestController
@RequestMapping("/admin/merParam/icemvinfo")
public class IcEmvInfoController extends BaseController {
    @Autowired
    private IcEmvInfoService icEmvInfoService;

    @GetMapping
//    @RequiresPermissions("areaInfo:view")
    public Map<String, Object> list(QueryRequest request, IcEmvInfo entity) {

        return getDataTable(this.icEmvInfoService.findByPage(request, entity));
    }

    @Log("查询单条")
    @GetMapping(value = "/view")
    @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    public IcEmvInfo getOne(@NotBlank(message = "{required}") String id) {
        IcEmvInfo entity = icEmvInfoService.getById(id);
        String val = entity.getParaVal();
			/*AID:9F0608A000000333010101
			应用选择指示符：DF010101
    		应用版本号：9F08020020
    		TAC缺省：DF1105FC78F4F8F0
    		TAC联机：DF1205FC78F4F8F0
    		TAC拒绝：DF13050010000000
    		终端最低限额：9F1B0400002710
    		偏置随机选择的阈值：DF150400000000
    		偏置随机选择的最大目标百分数：DF160120
    		随机选择的目标百分数：DF170110
    		缺省DDOL：DF14039F3704
    		终端联机PIN支持能力：DF180101
    		TAC拒绝tacjj
	 		终端最低限额zdzdxe
			偏置随机选择的最大百分数zdbfs
			偏置随机选择的百分数xzdbfs
			缺省DDOL qsddol
 			联机PIN能力pinnl
			终端电子现金交易限额 dzxjxe
	 		非接触读写器脱机最低限额 fdxqtjxe
			非接触读写器限额 fdxqxe;
	 		读写器持卡人验证方法(CVM)所需限额 yzffsxxe;*/


        entity.setRid(entity.getParaVal());
        entity.setAid(entity.getParaId()!=null?entity.getParaId().replaceAll(" ", ""):null);
        entity.setYyzsf(val.substring(28, 30));
        entity.setYybb(val.substring(36, 40));
        entity.setTacqs(val.substring(46, 56));
        entity.setTaclj(val.substring(62, 72));
        entity.setTacjj(val.substring(78, 88));
        Integer zdzdxe = Integer.parseInt(val.substring(94, 102), 16);
        entity.setZdzdxe(zdzdxe.toString());
        Integer fz = Integer.parseInt(val.substring(108, 116));
        entity.setFz(fz.toString());
        entity.setZdbfs(val.substring(122, 124));
        entity.setXzdbfs(val.substring(130, 132));
        entity.setQsddol(val.substring(138, 144));
        entity.setPinnl(val.substring(150, 152));

        if (val.length() > 152) {
            Integer dzxjxe = Integer.parseInt(val.substring(158, 170));
            String dzxjxe1 = dzxjxe.toString();
            entity.setDzxjxe(dzxjxe1);
        }
        if (val.length() > 170) {
            Integer fdxqtjxe = Integer.parseInt(val.substring(176, 188));
            String dzxjxe1 = fdxqtjxe.toString();
            entity.setFdxqtjxe(dzxjxe1);
        }
        if (val.length() > 188) {
            Integer fdxqxe = Integer.parseInt(val.substring(194, 206));
            String dzxjxe1 = fdxqxe.toString();
            entity.setFdxqxe(dzxjxe1);
        }
        if (val.length() > 206) {
            Integer yzffsxxe = Integer.parseInt(val.substring(212, 224));
            String dzxjxe1 = yzffsxxe.toString();
            entity.setYzffsxxe(dzxjxe1);
        }
        return entity;
    }

    @Log("编辑详情")
    @GetMapping(value = "/viewNew")
    @ApiImplicitParam(paramType = "query", name = "id", required = true, value = "id主键", dataType = "String")
    public IcEmvInfo editShowNew(@NotBlank(message = "{required}") String id) throws RedpException {
        IcEmvInfo entity = icEmvInfoService.getById(id);
        IcEmvInfoBean icpubkeyinfobean = JSON.parseObject(entity.getParaVal(), IcEmvInfoBean.class);
        entity.setYyzsf(icpubkeyinfobean.getASI());
        entity.setYybb(icpubkeyinfobean.getAPP_VER());
        entity.setTacqs(icpubkeyinfobean.getTAC_DEF());
        entity.setTaclj(icpubkeyinfobean.getTAC_OL());
        entity.setTacjj(icpubkeyinfobean.getTAC_REJECT());
        entity.setZdzdxe(icpubkeyinfobean.getLWM());
        entity.setFz(icpubkeyinfobean.getLAND_VAL());
        entity.setZdbfs(icpubkeyinfobean.getMAX_PCT());
        entity.setXzdbfs(icpubkeyinfobean.getTARGET_PCT());
        entity.setQsddol(icpubkeyinfobean.getDDOL_DEF());
        entity.setPinnl(icpubkeyinfobean.getPIN_ABILITY());
        entity.setDzxjxe(icpubkeyinfobean.getOFFLINE_HLM());
        entity.setFdxqtjxe(icpubkeyinfobean.getUNCONTACT_LLM());
        entity.setFdxqxe(icpubkeyinfobean.getUNCONTACT_HLM());
        entity.setYzffsxxe(icpubkeyinfobean.getCVM());
        return entity;
}

    @Log("新增")
    @PostMapping
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "paraId", value = "AID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "paraIdx", value = "参数索引", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "yyzsf", value = "应用指示符", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "yybb", value = "应用版本", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "tacqs", value = "TAC缺省", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "taclj", value = "TAC联机", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "tacjj", value = "TAC拒绝", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "zdzdxe", value = "终端最低限额(分)", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "fz", value = "偏置随机产生的阀值", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "zdbfs", value = "偏置随机选择的最大百分数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "xzdbfs", value = "偏置随机选择的百分数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "qsddol", value = "缺省DDOL", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pinnl", value = "联机PIN能力", required = true, dataType = "String")
    })
    public void add(@Valid IcEmvInfo entity) throws RedpException {
        try {
            this.icEmvInfoService.create(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("/update")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void update(@Valid IcEmvInfo entity) throws RedpException {
        try {
            this.icEmvInfoService.update(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("新增")
    @PostMapping("newedit")
//    @RequiresPermissions("areaInfo:add")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "paraId", value = "AID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "paraIdx", value = "参数索引", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "yyzsf", value = "应用指示符", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "yybb", value = "应用版本", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "tacqs", value = "TAC缺省", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "taclj", value = "TAC联机", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "tacjj", value = "TAC拒绝", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "zdzdxe", value = "终端最低限额(分)", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "fz", value = "偏置随机产生的阀值", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "zdbfs", value = "偏置随机选择的最大百分数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "xzdbfs", value = "偏置随机选择的百分数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "qsddol", value = "缺省DDOL", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pinnl", value = "联机PIN能力", required = true, dataType = "String")
    })
    public void newadd(@Valid IcEmvInfo entity) throws RedpException {
        try {

           /* User user = RedpUtil.getCurrentUser();
            String crtOrgId = user.getOrgCode();
            if (user.isAdmin()) {
                if (StringUtils.isEmpty(entity.getInstId())) {
                    log.error("保存数据时请先选择法人行!");
                    throw new RedpException("保存数据时请先选择法人行！");
                }
            }*/
            this.icEmvInfoService.newCreate(entity);
        } catch (Exception e) {
            String message = "新增失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改")
    @PutMapping("newedit")
//    @RequiresPermissions("areaInfo:update")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id主键", required = true, dataType = "String")
    })
    public void newedit(@Valid IcEmvInfo entity) throws RedpException {
        try {
                 /* User user = RedpUtil.getCurrentUser();
            String crtOrgId = user.getOrgCode();
            if (user.isAdmin()) {
                if (StringUtils.isEmpty(entity.getInstId())) {
                    log.error("保存数据时请先选择法人行!");
                    throw new RedpException("保存数据时请先选择法人行！");
                }
            }*/
            this.icEmvInfoService.newUpdate(entity);
        } catch (Exception e) {
            String message = "修改失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("删除")
    @DeleteMapping("/{ids}")
//    @RequiresPermissions("areaInfo:delete")
    @ApiImplicitParam(paramType = "path", name = "ids", value = "id主键，逗号隔开", required = true, dataType = "String")
    public void delete(@NotBlank(message = "{required}") @PathVariable String ids) throws RedpException {
        try {
            this.icEmvInfoService.delete(ids.split(StringPool.COMMA));
        } catch (Exception e) {
            String message = "删除失败";
            log.error(message, e);
            throw new RedpException(message);
        }
    }


    @Log("新增IC卡参数")
    @PostMapping("addIcemv")
    public void addIcemv(IcEmvInfo icEmvInfo) throws RedpException{
        try {
            this.icEmvInfoService.addIcemv(icEmvInfo);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }
    }

    @Log("修改IC卡参数")
    @PutMapping("updIcemv")
    public void updIcemv(IcEmvInfo icEmvInfo) throws RedpException{
        try {
            this.icEmvInfoService.updIcemv(icEmvInfo);
        } catch (Exception e) {
            String message = e.getMessage();
            log.error(message, e);
            throw new RedpException(message);
        }
    }



}
